package com.fbs.wowo.chat.mapper;

import com.fbs.wowo.chat.entity.CrewMember;
import com.fbs.wowo.chat.vo.CrewMemberVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface CrewMemberMapper {
    @Delete({
        "delete from crew_member",
        "where uid = #{uid,jdbcType=VARCHAR}",
          "and cid = #{cid,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(@Param("uid") String uid, @Param("cid") String cid);

    @Insert({
        "insert into crew_member (uid, cid, ",
        "nickname, valid, ",
        "shield, create_time, ",
        "update_time)",
        "values (#{uid,jdbcType=VARCHAR}, #{cid,jdbcType=VARCHAR}, ",
        "#{nickname,jdbcType=VARCHAR}, #{valid,jdbcType=TINYINT}, ",
        "#{shield,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP}, ",
        "#{updateTime,jdbcType=TIMESTAMP})"
    })
    int insert(CrewMember record);

    @InsertProvider(type=CrewMemberSqlProvider.class, method="insertSelective")
    int insertSelective(CrewMember record);

    @Select({
        "select",
        "uid, cid, nickname, valid, shield, create_time, update_time",
        "from crew_member",
        "where uid = #{uid,jdbcType=VARCHAR}",
          "and cid = #{cid,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="uid", property="uid", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="cid", property="cid", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="nickname", property="nickname", jdbcType=JdbcType.VARCHAR),
        @Result(column="valid", property="valid", jdbcType=JdbcType.TINYINT),
        @Result(column="shield", property="shield", jdbcType=JdbcType.TINYINT),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    CrewMember selectByPrimaryKey(@Param("uid") String uid, @Param("cid") String cid);

    @UpdateProvider(type=CrewMemberSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(CrewMember record);

    @Update({
        "update crew_member",
        "set nickname = #{nickname,jdbcType=VARCHAR},",
          "valid = #{valid,jdbcType=TINYINT},",
          "shield = #{shield,jdbcType=TINYINT},",
          "create_time = #{createTime,jdbcType=TIMESTAMP},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP}",
        "where uid = #{uid,jdbcType=VARCHAR}",
          "and cid = #{cid,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(CrewMember record);

    /**
     * 群成员列表
     * @param cid
     * @return
     */
    @Select({
            "select",
            "cm.uid, cm.cid, cm.nickname, u.name, cm.valid, cm.shield, u.photo, cm.create_time, cm.update_time",
            "from crew_member cm",
            "inner join user u",
            "on cm.uid = u.user_id",
            "where cm.cid = #{cid,jdbcType=VARCHAR} and cm.valid = 1"
    })
    @Results({
            @Result(column="uid", property="uid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="cid", property="cid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="nickname", property="nickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="valid", property="valid", jdbcType=JdbcType.TINYINT),
            @Result(column="shield", property="shield", jdbcType=JdbcType.TINYINT),
            @Result(column="photo", property="photo", jdbcType=JdbcType.TINYINT),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<CrewMemberVo> crewMemberList(String cid);

    /**
     * 按群组id查找成员
     * @param cid
     * @return
     */
    @Select({
            "select",
            "uid, cid, nickname, valid, shield, create_time, update_time",
            "from crew_member",
            "where cid = #{cid,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="uid", property="uid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="cid", property="cid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="nickname", property="nickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="valid", property="valid", jdbcType=JdbcType.TINYINT),
            @Result(column="shield", property="shield", jdbcType=JdbcType.TINYINT),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<CrewMember> findByCid(String cid);

    /**
     * 解散群组，设置所有好友关系valid = 0
     * @param cid
     */
    @Update({
            "update crew_member set valid=0 where cid = #{cid,jdbcType=VARCHAR}"
    })
    void removeCrew(String cid);

    /**
     * 查找群成员 只取审核的
     * @param crewId 群组id
     * @param userId 用户id
     * @return 群组成员
     */
    @Select({
            "select",
            "uid, cid, nickname, valid, shield, create_time, update_time",
            "from crew_member",
            "where uid = #{uid,jdbcType=VARCHAR}",
            "and cid = #{cid,jdbcType=VARCHAR} and valid = 1"
    })
    @Results({
            @Result(column="uid", property="uid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="cid", property="cid", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="nickname", property="nickname", jdbcType=JdbcType.VARCHAR),
            @Result(column="valid", property="valid", jdbcType=JdbcType.TINYINT),
            @Result(column="shield", property="shield", jdbcType=JdbcType.TINYINT),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    CrewMember selectByPrimaryKeyValid(@Param("uid") String userId, @Param("cid") String crewId);
}